<template>
  <div>
    <!--列表-->
    <el-table :data="userList" :default-sort="{ prop: 'lastLoginTime', order:'descending'}">
      <el-table-column label="id" prop="id" sortable></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="微信头像">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="性别" prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex === 1 ? "男" : scope.row.sex === 2 ? "女" : "未知" }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="phone" sortable></el-table-column>
      <el-table-column label="最近登录" prop="lastLoginTime" sortable>
        <template slot-scope="scope">
          {{
            scope.row.lastLoginTime
              ? $dateUtil.fmtDate(scope.row.lastLoginTime * 1000)
              : "无"
          }}
        </template>
      </el-table-column>
      <el-table-column label="注册时间" prop="createTime" sortable>
        <template slot-scope="scope">
          {{
            scope.row.createTime
              ? $dateUtil.fmtDate(scope.row.createTime * 1000)
              : "无"
          }}
        </template>
      </el-table-column>
      <el-table-column label="账号状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status === 1"
            active-color="green"
            inactive-color="red"
            :disabled="scope.row.status === 3"
            @change="updateUserStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-row style="float: right">
      <el-pagination
        @current-change="changePageNo"
        @size-change="changePageSize"
        :current-page.sync="searchParams.pageNo"
        :page-size.sync="searchParams.pageSize"
        :page-sizes="[1, 5, 10, 50, 250]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as userAdminApi from "../api/userAdminApi";

export default {
  name: "userAdminIndex",
  data() {
    return {
      // 查询参数
      searchParams: {
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,
      userList: [],
    };
  },
  mounted() {
    this.getUserList();
  },
  methods: {
    // 获取数据
    getUserList() {
      userAdminApi.getUserList({
        pageNo: this.searchParams.pageNo,
        pageSize: this.searchParams.pageSize,
        success: (result) => {
          this.userList = result.data.list;
          this.total = result.data.total;
        },
      });
    },
    // 切换页数
    changePageNo(pageNo) {
      this.searchParams.pageNo = pageNo;
      this.getUserList();
    },
    // 改变页面大小
    changePageSize(pageSize) {
      this.searchParams.pageSize = pageSize;
      this.getUserList();
    },
    // 改变账号状态
    updateUserStatus(user) {
      let newStatus = user.status === 1 ? 2 : 1;
      userAdminApi.updateUserStatus({
        userId: user.id,
        newStatus: newStatus,
        success: () => {
          user.status = newStatus;
          if (newStatus == 1) {
            this.$message.success("账号已激活");
          } else if (newStatus == 2) {
            this.$message.success("账号已冻结");
          }
        },
      });
    },
  },
};
</script>
